package com.graphics.ui;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.drawable.BitmapDrawable;
import android.util.AttributeSet;
import android.view.View;

import com.bin.activities.R;
import com.bin.comunication.ComunicationConnection;
import com.bin.intent1.PowerSlider;
import com.bin.intent1.PowerSliderBluetooth;

public class RightSideView extends View{
	private BitmapDrawable image;
	
	private int textHeight;
	private Paint textPaint;
	private float heading;
    private float pitch,offsetPitch;
    private float roll,offsetRoll;
    
    private Matrix matrix;
    private Bitmap bitmapOrg;
    private float tolerance=0f;
    
    private PowerSlider ps;
    @SuppressWarnings("unused")
	private PowerSliderBluetooth psb;
 //   private boolean initValues=false;
    private ComunicationConnection mComunication=null;
    private String temperature="";
    
    public void setComunicationConnection(ComunicationConnection obj){
    	mComunication=obj;
    }
    
    public void setTemperature(String temp){
    	this.temperature=temp; 
 //   	this.invalidate();
    }
    
    private float adjustData(float value, float offset){
    	value-=offset;
    	
    	if(value>90){
    		value=90;
    	}
    	if(value<-90){
    		value=-90;
    	}
    	return value;
    }
    public void setData(float data[]){
    	if(mComunication==null) return;
    	if(ps==null){
    		ps=PowerSlider.getInstance();
    		return;
    	}
    	if (data==null||data.length!=3) return;
    	if(Math.abs(this.pitch-data[2])<tolerance&&Math.abs(this.roll-data[1])<tolerance) return;
    	this.heading=data[0]; this.roll=data[1]; this.pitch=data[2]; 
    	
    	this.roll=adjustData(data[1],offsetRoll);
    	this.pitch=adjustData(data[2],offsetPitch);
   /* 	
    	if(initValues==false){
    			psb=new PowerSliderBluetooth(ps,mComunication);
    			initValues=true;
     		
    	}
*/
        ps.setStatus(this.roll,this.pitch);
        this.invalidate();
    }
    
    

    public RightSideView(Context context) {
		super(context);	
		initRightSideView(context);
	}
    
    public RightSideView(Context context, AttributeSet attrs){
    	super(context,attrs); 
    	initRightSideView(context);
    }

    public RightSideView(Context context, AttributeSet attrs,int defaultStyle){
    	super(context,attrs,defaultStyle);
    	initRightSideView(context);    	
    }
   
    
    protected void onMeasure(int widthMeasureSpec,int heightMeasureSpec){
    	
    	int measuredWidth =measure(widthMeasureSpec);
    	int measuredHeight=measure(heightMeasureSpec);
    		
    	setMeasuredDimension(measuredWidth,measuredHeight);
    }
    
    private int measure(int measureSpec) {
		
		int specMode=MeasureSpec.getMode(measureSpec);
		int specSize=MeasureSpec.getSize(measureSpec);
		
		if(specMode==MeasureSpec.UNSPECIFIED){
			return 200;
		}else{
			return specSize;
		}
	}
    
    protected void initRightSideView(Context context){
        Resources r=this.getResources();
        bitmapOrg = BitmapFactory.decodeResource(r,R.drawable.colorhelicopter);
    	matrix = new Matrix();
    	float size;
    	
        textPaint=new Paint(Paint.ANTI_ALIAS_FLAG);
        textPaint.setColor(r.getColor(R.color.text_color));
        size=textPaint.getTextSize();
        size*=1.5;
        textPaint.setTextSize(size); textPaint.setFakeBoldText(true);
        textHeight=(int)textPaint.measureText("yY");
        CustomImageButton.registerView(this);    
    }
    
    public void setOrigin(boolean status){
    	if(status==false){
    		offsetPitch=pitch;
    		offsetRoll=roll;
    		roll-=offsetRoll; pitch-=offsetPitch; 
    	}else{
    		roll+=offsetRoll; pitch+=offsetPitch;
    		offsetPitch=0; offsetRoll=0; 
    	}
    }
    
    @Override
    protected void onDraw(Canvas canvas){        
    	Resources r=this.getResources();
    	
    	int width =(int) getMeasuredWidth();
        int height=(int) getMeasuredHeight(); 
    	int dimension;
    	String value;
    	float pozY,pozX;
    		try{dimension=Integer.parseInt(r.getString(R.string.PictureDimension));}catch(NumberFormatException e) {dimension=150;}
    		matrix.postRotate(-this.roll,bitmapOrg.getWidth()/2,bitmapOrg.getHeight()/2);
    		Bitmap rotatedBitmap = Bitmap.createBitmap(bitmapOrg, 0, 0,bitmapOrg.getWidth(), bitmapOrg.getHeight(), matrix, true);
    		image = new BitmapDrawable(rotatedBitmap);
    		image.setBounds(width-dimension, height-dimension, width,height);
    	
    		image.draw(canvas);
    		matrix.postRotate(this.roll,bitmapOrg.getWidth()/2,bitmapOrg.getHeight()/2);
    		canvas.rotate(90);
  
    		pozX=0; pozY=height-textHeight;
        	canvas.drawText("Temperature:"+temperature, -pozX, -pozY, textPaint);
          	
    	pozX=0; pozY-=2*textHeight; value=this.heading+"";
    	canvas.drawText("Heading:"+value, -pozX, -pozY, textPaint);
    	
    	pozX=0; pozY-=textHeight+5; value=this.pitch+"";
    	canvas.drawText("Pitch:"+value, -pozX, -pozY, textPaint);
    	
    	pozX=0; pozY-=textHeight+5; value=this.roll+"";
    	canvas.drawText("Roll:"+value, -pozX, -pozY, textPaint);
        
    	pozX=0; pozY-=textHeight+20; value=this.temperature;
    	
    	canvas.restore();
    	
    }
    
}
